Imports ccapps.persistent.Telnet
Imports ccapps.persistent.Telnet.Scripting
Imports ccapps.persistent.FTP
Imports System.Data.OleDb
Imports System.Data.OracleClient
Imports System.IO

Public Class FUSDataRoaming
    Inherits [lib].BasePage

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Lblok As System.Web.UI.WebControls.Label
    Protected WithEvents ContentPaneFooter1 As ccapps.BillingNetControlLibrary.ContentPaneFooter
    Protected WithEvents RequiredfieldvalidatorThreshold As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents PanelFUSSummary As System.Web.UI.WebControls.Panel
    Protected WithEvents LabelMSISDNDesc As System.Web.UI.WebControls.Label
    Protected WithEvents LabelMSISDN As System.Web.UI.WebControls.Label
    Protected WithEvents LabelThresholdDesc As System.Web.UI.WebControls.Label
    Protected WithEvents LabelThreshold As System.Web.UI.WebControls.Label
    Protected WithEvents LabelNotifyDesc As System.Web.UI.WebControls.Label
    Protected WithEvents CheckBoxNotify As System.Web.UI.WebControls.CheckBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    'Private _PointingLinks() As String = New String() {"RerateCustBulkEditTypeSelect.aspx"}

    Shared coID As String = ""
    Shared MSISDN As String = ""
    Shared custCode As String = ""
    Shared customerType As Integer = 0
    Shared resultFlag As Integer = 0
    Shared errorCode As Integer = -1
    Shared errorDesc As String = "-"
    Shared operation As String
    Shared type As String = ""
    Shared val As String = ""
    Shared errorMessage As String

    Shared pThreholdTypeCode As String ',-- OUT VARCHAR2,
    Shared pDescription As String ',-- OUT VARCHAR2,
    Shared pImplicitRegistrationFlag As String ',-- OUT VARCHAR2,
    Shared pMultipleNotificationsFlag As String ',-- OUT VARCHAR2,
    Shared pCustomThresholdAllowedFlag As String ',-- OUT VARCHAR2,
    Shared pMinAllowedAmount As Double  ',-- OUT FLOAT,
    Shared pMaxAllowedAmount As Double  ',-- OUT FLOAT,
    'pErrorCode,-- OUT NUMBER,
    'pErrorDesc-- OUT VARCHAR2
    Shared pFus_Flag As String

    'pcustcode IN VARCHAR2,
    'Shared pMinThresholdId As Integer = 0 'pMinThresholdId OUT fus_threshold_type.threshold_type_id%TYPE,
    'Shared pMinThresholdCustom As Double = 0 'pMinThresholdCustom OUT FLOAT,
    'Shared pRecieveAllNotifications As String = "" 'pRecieveAllNotifications OUT VARCHAR2,
    Shared pTimeStamp As DateTime = DateTime.Now 'pTimeStamp OUT DATE,
    Shared pUnRegistered As String = "" 'pRegistered OUT VARCHAR2,
    'pErrorCode OUT NUMBER,
    'pErrorDesc OUT VARCHAR2

    Private Shared ConnStr As String = GlobalVar.OraConnStr


    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.PageTitle = "Fatura Ust Siniri (FUS) >> Data Roaming Ust Sinir"

        If Not Page.IsPostBack Then

            operation = Request.QueryString.Get("Operation")
            type = Request.QueryString.Get("Type")
            val = Request.QueryString.Get("Val")

            If (type = "Cust") Then
                custCode = val
                LabelMSISDNDesc.Text = "Uyari SMS'i gonderiminde kullanilan MSISDN listesi :"
            Else
                MSISDN = val
                LabelMSISDNDesc.Text = "Uyari SMS'i gonderiminde kullanilan MSISDN :"
            End If

            fillMSISDN(type, val)
            prepareThresholdLabel(type, val)
        End If
    End Sub

    Private Sub fillMSISDN(ByVal type As String, ByVal val As String)
        Select Case type
            Case "Cust"
                fillMSISDNLabelViaCustCode(val, False)
                'Case "Co"
                'fillDataGridViaCoID()
            Case "MS"
                fillMSISDNLabelViaMSISDN(val)
            Case Else
                Exit Select
        End Select
    End Sub

    Private Sub fillMSISDNLabelViaCustCode(ByVal _custCode As String, ByVal checkMSISDN As Boolean)

        Try
            customerType = CType(persistent.myDBConn.ORAScalar("SELECT FUS_REGISTRATION_API.get_fus_customer_type(null,'" + _custCode + "') FROM DUAL"), Integer)

            Dim responsibleList As DataSet
            Dim parameterNames As String() = New String() {"pcustcode", "pResponsibleList", "perrorcode", "perrordesc"}
            Dim parameterTypes As OracleType() = New OracleType() {OracleType.VarChar, OracleType.Cursor, OracleType.Number, OracleType.VarChar}
            Dim parameterValues As Object() = New Object() {_custCode, responsibleList, errorCode, errorDesc}
            Dim parameterDirections As ParameterDirection() = New ParameterDirection() {ParameterDirection.Input, ParameterDirection.Output, ParameterDirection.Output, ParameterDirection.Output, ParameterDirection.Output}

            If customerType = 1 Then
                If checkMSISDN Then
                    LabelMSISDN.Text = MSISDN
                Else
                    Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Mass aboneler icin lutfen MSISDN giriniz !'); javascript:history.go(-1) </SCRIPT>")
                End If
                'ORAProcedure_Generic_INOUT("FUS_REGISTRATION_API.read_firm_responsibles", parameterNames, parameterTypes, parameterDirections, parameterValues)
                'persistent.myDBConn.ORAProcedure_INOUT("FUS_REGISTRATION_API.read_firm_responsibles", parameterNames, parameterValues)
                'Dim query As String = "select distinct coa.co_id as CoID, fn_coid_to_msisdn(coa.co_id) as MSISDN from customer_all cua, contract_all coa where(coa.customer_id = cua.customer_id) and  (cua.custcode = '" & custCode & "' or cua.custcode like '%" & custCode & "%')"
                'Me.DataGridCustList.DataSource = persistent.myDBConn.myORATable(query)
                'MSISDN = CType(parameterValues(1), DataSet).Tables(0).Rows(0).Item(1)
            ElseIf customerType = 2 Then

                ORAProcedure_Generic_INOUT("FUS_REGISTRATION_API.read_firm_responsibles", parameterNames, parameterTypes, parameterDirections, parameterValues)

                If CType(parameterValues(1), DataSet).Tables(0).Rows.Count < 1 Then
                    Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone bulunamadi.'); javascript:history.go(-1) </SCRIPT>")
                End If
                For Each msisdnRow As DataRow In CType(parameterValues(1), DataSet).Tables(0).Rows
                    If Not msisdnRow.Item("FUS_FLAG") Is DBNull.Value Then
                        If CType(msisdnRow.Item("FUS_FLAG"), String) = "X" Then
                            LabelMSISDN.Text += CType(msisdnRow.Item("MSISDN"), String) & "<br>"
                        End If
                    End If
                Next

            ElseIf customerType = -1 Then
                Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone bulunamadi veya aktif degil !'); javascript:history.go(-1) </SCRIPT>")
            ElseIf customerType = -2 Then
                Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone payment responsible degil !'); javascript:history.go(-1) </SCRIPT>")
            Else
                Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone bulunamadi !'); javascript:history.go(-1) </SCRIPT>")
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

    Private Sub fillMSISDNLabelViaMSISDN(ByVal _MSISDN As String)
        Try
            MSISDN = _MSISDN
            customerType = CType(persistent.myDBConn.ORAScalar("SELECT FUS_REGISTRATION_API.get_fus_customer_type('" + _MSISDN + "',null) FROM DUAL"), Integer)

            If customerType = 1 Then
                Try
                    custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & _MSISDN & "') from dual")
                    'fillMSISDNLabelViaCustCode(custCode, True)
                    LabelMSISDN.Text = MSISDN
                Catch ex As Exception
                    If MSISDN.Length = 10 Then
                        MSISDN = "90" + MSISDN
                    End If
                    Try
                        custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & _MSISDN & "') from dual")
                        'fillMSISDNLabelViaCustCode(custCode, True)
                        LabelMSISDN.Text = MSISDN
                    Catch exc As Exception
                        If custCode = "" Then
                            Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Aboneye ait custcode bulunamadi !'); javascript:history.go(-1) </SCRIPT>")
                            Exit Sub
                        End If
                    End Try
                End Try
            ElseIf customerType = 2 Then
                Try
                    custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & _MSISDN & "') from dual")
                    'fillMSISDNLabelViaCustCode(custCode, True)
                    LabelMSISDN.Text = MSISDN
                Catch ex As Exception
                    If MSISDN.Length = 10 Then
                        MSISDN = "90" + MSISDN
                    End If
                    Try
                        custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & _MSISDN & "') from dual")
                        'fillMSISDNLabelViaCustCode(custCode, True)
                        LabelMSISDN.Text = MSISDN
                    Catch exc As Exception
                        If custCode = "" Then
                            Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Aboneye ait custcode bulunamadi !'); javascript:history.go(-1) </SCRIPT>")
                            Exit Sub
                        End If
                    End Try
                End Try
                'Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone mass degil !'); javascript:history.go(-1) </SCRIPT>")
            ElseIf customerType = -1 Then
                Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone bulunamadi veya aktif degil !'); javascript:history.go(-1) </SCRIPT>")
            ElseIf customerType = -2 Then
                Try
                    custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & _MSISDN & "') from dual")
                    'fillMSISDNLabelViaCustCode(custCode, True)
                    LabelMSISDN.Text = MSISDN
                Catch ex As Exception
                    If MSISDN.Length = 10 Then
                        MSISDN = "90" + MSISDN
                    End If
                    Try
                        custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & _MSISDN & "') from dual")
                        'fillMSISDNLabelViaCustCode(custCode, True)
                        LabelMSISDN.Text = MSISDN
                    Catch exc As Exception
                        If custCode = "" Then
                            Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Aboneye ait custcode bulunamadi !'); javascript:history.go(-1) </SCRIPT>")
                            Exit Sub
                        End If
                    End Try
                End Try
                'Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone payment responsible degil !'); javascript:history.go(-1) </SCRIPT>")
            Else
                Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Abone bulunamadi !'); javascript:history.go(-1) </SCRIPT>")
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try

    End Sub

    Private Sub prepareThresholdLabel(ByVal type As String, ByVal val As String)
        Select Case type
            Case "Cust"
                prepareThresholdLabelViaCustCode(val)
                'Case "Co"
                'fillDataGridViaCoID()
            Case "MS"
                prepareThresholdLabelViaMSISDN(val)
            Case Else
                Exit Select
        End Select
    End Sub

    Private Sub prepareThresholdLabelViaCustCode(ByVal val As String)
        Try
            custCode = val
            pUnRegistered = ""
            pTimeStamp = New DateTime

            Dim parameterNames As String() = New String() {"pmsisdn", "pTimeStamp", "pUnRegistered", "perrorcode", "perrordesc"}
            Dim parameterTypes As OracleType() = New OracleType() {OracleType.VarChar, OracleType.DateTime, OracleType.VarChar, OracleType.Number, OracleType.VarChar}
            Dim parameterValues As Object() = New Object() {MSISDN, pTimeStamp, pUnRegistered, errorCode, errorDesc}
            Dim parameterDirections As ParameterDirection() = New ParameterDirection() {ParameterDirection.Input, ParameterDirection.Output, ParameterDirection.Output, ParameterDirection.Output, ParameterDirection.Output}

            ORAProcedure_Generic_INOUT("FUS_REGISTRATION_API.read_roaming_registration_info", parameterNames, parameterTypes, parameterDirections, parameterValues)
            pUnRegistered = CType(parameterValues(2), String)
            pTimeStamp = CType(CType(parameterValues(1), String), Date)

            If Not pUnRegistered = "X" Then
                CheckBoxNotify.Checked = True
            Else
                CheckBoxNotify.Checked = False
            End If
            'custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & MSISDN & "') from dual")

            Dim thresholdList As DataSet
            parameterNames = New String() {"pThresholdType", "pThrecsholdList", "perrorcode", "perrordesc"}
            parameterTypes = New OracleType() {OracleType.Int32, OracleType.Cursor, OracleType.Number, OracleType.VarChar}
            parameterValues = New Object() {3, thresholdList, errorCode, errorDesc}
            parameterDirections = New ParameterDirection() {ParameterDirection.Input, ParameterDirection.Output, ParameterDirection.Output, ParameterDirection.Output, ParameterDirection.Output}

            ORAProcedure_Generic_INOUT("FUS_REGISTRATION_API.read_default_thresholds", parameterNames, parameterTypes, parameterDirections, parameterValues)

            For Each row As DataRow In CType(parameterValues(1), DataSet).Tables(0).Rows
                LabelThreshold.Text += CType(row.Item("amount"), String) & " TL" & "<br>"
            Next


        Catch ex As Exception
            Response.Write(ex.Message)
            'Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('Custcode bulunamadi veya hatali. Lutfen kontrol ediniz. !'); javascript:history.go(-1) </SCRIPT>")
        End Try
    End Sub

    Private Sub prepareThresholdLabelViaMSISDN(ByVal val As String)
        Try
            Dim _MSISDN As String = val
            custCode = persistent.myDBConn.ORAScalar("select fn_msisdn_to_custcode('" & _MSISDN & "') from dual")

            prepareThresholdLabelViaCustCode(custCode)

        Catch ex As Exception
            Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('MSISDN bulunamadi veya hatali. Lutfen kontrol ediniz. !'); javascript:history.go(-1) </SCRIPT>")
        End Try


    End Sub

    Private Sub ContentPaneFooter1_Confirmed(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContentPaneFooter1.Confirmed
        'pcustcode IN VARCHAR2,
        'pChannel IN VARCHAR2,
        'pUserId IN VARCHAR2,
        'pDealerId IN VARCHAR,
        'pErrorCode OUT NUMBER,
        'pErrorDesc OUT VARCHAR2
        Try
            Dim success As Boolean = False
            Dim parameterNames As String()
            Dim parameterTypes As OracleType()
            Dim parameterValues As Object()
            Dim parameterDirections As ParameterDirection()

            Dim pCustCode As String = custCode
            Dim pMSISDN As String = MSISDN
            Dim pChannel As String = "Billing.Net"
            Dim pUserId As String = Session.Item("user")
            Dim pDealerId As String

            parameterNames = New String() {"pmsisdn", "pChannel", "pUserId", "pDealerId", "pErrorCode", "pErrorDesc"}
            parameterTypes = New OracleType() {OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.VarChar, OracleType.Number, OracleType.VarChar}
            parameterValues = New Object() {pMSISDN, pChannel, pUserId, pDealerId, errorCode, errorDesc}
            parameterDirections = New ParameterDirection() {ParameterDirection.Input, ParameterDirection.Input, ParameterDirection.Input, ParameterDirection.Input, ParameterDirection.Output, ParameterDirection.Output}

            If CheckBoxNotify.Checked = True Then
                If pUnRegistered = "" Then
                    Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('FUS Data Roaming bilgilerini degistirmediniz. '); javascript:history.go(-1) </SCRIPT>")
                    Exit Sub
                End If
                ORAProcedure_Generic_INOUT("FUS_REGISTRATION_API.reregister_roaming", parameterNames, parameterTypes, parameterDirections, parameterValues)
            Else
                If pUnRegistered = "X" Then
                    Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('FUS Data Roaming bilgilerini degistirmediniz. '); javascript:history.go(-1) </SCRIPT>")
                    Exit Sub
                End If
                ORAProcedure_Generic_INOUT("FUS_REGISTRATION_API.unregister_roaming", parameterNames, parameterTypes, parameterDirections, parameterValues)
            End If

            If CType(parameterValues(4), Integer) = 0 Then
                'Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('FUS Data Roaming kaydiniz basariyla degistirildi. '); javascript:history.go(-1) </SCRIPT>")
                Response.Redirect("FUSResult.aspx?message=FUS Data Roaming kaydiniz basariyla degistirildi. ", False)
            Else
                Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('FUS Data Roaming islemi basarisiz !'); javascript:history.go(-1) </SCRIPT>")
                'Response.Write(CType(parameterValues(5), String))
                Session.Add("checkRequestResult", CType(parameterValues(5), String))
                Response.Redirect("FUSResult.aspx?message=session", False)
            End If
        Catch ex As Exception
            Page.RegisterStartupScript("1231234122", "<SCRIPT LANGUAGE=""JAVASCRIPT"">alert('FUS Data Roaming islemi basarisiz !'); javascript:history.go(-1) </SCRIPT>")
            Session.Add("checkRequestResult", ex.Message)
            Response.Redirect("FUSResult.aspx?message=session", False)
        End Try

    End Sub

    Public Function ORAProcedure_Generic_INOUT(ByVal p_ProcedureFullName As String, ByVal p_Parameters_Def_Array() As String, ByVal p_Parameters_Type_Array() As OracleType, ByVal p_Parameters_Direction_Array() As ParameterDirection, ByRef p_Parameters_Val_Array() As Object) As Object

        Dim connection As New OracleConnection(ConnStr)
        Dim command As New OracleCommand
        Dim reader As OracleDataReader
        connection.Open()
        command.Connection = connection
        command.CommandText = p_ProcedureFullName
        command.CommandType = CommandType.StoredProcedure
        Dim isDataSetExists As Boolean = False

        Dim i As Integer
        For i = 0 To p_Parameters_Def_Array.Length - 1
            Dim l_curparam As New OracleParameter(p_Parameters_Def_Array(i), p_Parameters_Type_Array(i))
            l_curparam.Direction = p_Parameters_Direction_Array(i)

            If (p_Parameters_Type_Array(i) = OracleType.VarChar Or p_Parameters_Type_Array(i) = OracleType.NVarChar) Then
                l_curparam.Size = 4000
            End If
            If p_Parameters_Val_Array(i) Is Nothing Then
                l_curparam.Value = DBNull.Value
            ElseIf (p_Parameters_Type_Array(i) = OracleType.VarChar Or p_Parameters_Type_Array(i) = OracleType.NVarChar) Then
                If (p_Parameters_Val_Array(i) = "") Then
                    l_curparam.Value = DBNull.Value
                Else
                    l_curparam.Value = p_Parameters_Val_Array(i)
                End If
            ElseIf p_Parameters_Type_Array(i) = OracleType.Double Or p_Parameters_Type_Array(i) = OracleType.Float Or p_Parameters_Type_Array(i) = OracleType.Number Then
                If (p_Parameters_Val_Array(i) = 0) Then
                    l_curparam.Value = DBNull.Value
                Else
                    l_curparam.Value = p_Parameters_Val_Array(i)
                End If
            Else
                l_curparam.Value = p_Parameters_Val_Array(i)
            End If
            'command.Parameters.Add(l_curparam).Value = IIf((Not p_Parameters_Val_Array(i) Is Nothing), DBNull.Value, p_Parameters_Val_Array(i))
            command.Parameters.Add(l_curparam)
        Next
        Dim ODA As New OracleDataAdapter(command)
        ODA.SelectCommand = command
        For i = 0 To p_Parameters_Def_Array.Length - 1
            If (p_Parameters_Type_Array(i) = OracleType.Cursor) Then
                p_Parameters_Val_Array(i) = New DataSet("results")
                'reader = command.ExecuteReader()
                ODA.Fill(p_Parameters_Val_Array(i), "results")
                isDataSetExists = True
                Exit For
            End If
        Next
        If Not isDataSetExists Then
            command.ExecuteNonQuery()
            'reader = command.ExecuteReader()
        End If
        For i = command.Parameters.Count - 1 To 0 Step -1
            If p_Parameters_Direction_Array(i) = ParameterDirection.Input Then
                Exit For
            End If
            If Not command.Parameters(i).Value Is DBNull.Value Then
                p_Parameters_Val_Array(i) = command.Parameters(i).Value
            End If
        Next

        connection.Close()
    End Function
End Class
